package com.hackerrank.challenges.thequickestwayup;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Solution {
	private static final int TARGET_SQUARE = 100;
	private static class Target {
		int n, m;
		public Target(int n, int m) {
			this.n = n;
			this.m = m;
		}
	}
	private static int climb(int j[], boolean v[], int s) {
		v[s] = true;
		while(j[s] != 0) {
			s += j[s];
			v[s] = true;
		}
		return s;
	}
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int T = in.nextInt();
		in.nextLine();
		for(int t = 0; t < T; t++) {
			in.nextLine();
			int j[] = new int[TARGET_SQUARE + 1];
			String L[] = in.nextLine().split("\\s+");
			for(int i = 0; i < L.length; i++) {
				String r[] = L[i].split(",");
				int s = Integer.parseInt(r[0]);
				j[s] = Integer.parseInt(r[1]) - s;
			}
			String S[] = in.nextLine().split("\\s+");
			for(int i = 0; i < S.length; i++) {
				String r[] = S[i].split(",");
				int s = Integer.parseInt(r[0]);
				j[s] = Integer.parseInt(r[1]) - s;
			}
			boolean v[] = new boolean[TARGET_SQUARE + 1];
			Queue<Target> q = new LinkedList<Target>();
			Target ct = new Target(climb(j, v, 0), 0);
			while(ct != null && ct.n < TARGET_SQUARE) {
				for(int i = 1; i <= 6; i++)
					if(ct.n + i <= TARGET_SQUARE && !v[ct.n + i])
						q.add(new Target(climb(j, v, ct.n + i), ct.m + 1));
				ct = q.poll();
			}
			System.out.println(ct.m);
		}
	}
}
